package com.bridgeintelligent.tag.webserver.dim.mgmt.service.department;

import com.bridgeintelligent.tag.mapper.DepartmentMapper;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.bridgeintelligent.tag.utils.SecurityHelper;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.BaseCode;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimDepartment;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimRep;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by JackCheung on 2019/9/13 9:27 PM.
 */
@Service
public class DepartmentService implements IDepartmentService {

    private DepartmentMapper departmentMapper;

    public DepartmentService(DepartmentMapper departmentMapper) {
        this.departmentMapper = departmentMapper;
    }

    @Override
    public DimRep<List<DimDepartment>> list() {
        List<DimDepartment> depts = departmentMapper.list();
        User user = SecurityHelper.currentUser();
        String selfDeptId = user.getDepartment();
        depts.stream().filter(dept -> dept.getId().equals(selfDeptId)).peek(dept -> dept.setOwnDept(true));
        return new DimRep<>(departmentMapper.list(), BaseCode.SUCCESS);
    }

    @Override
    public DimRep listInDims() {
        List<DimDepartment> result = departmentMapper.listInDims().stream().filter(Objects::nonNull).collect(Collectors.toList());
        return new DimRep<>(result, BaseCode.SUCCESS);
    }

    @Override
    public DimDepartment fetchByName(String name) {
        return departmentMapper.findOneByName(name);
    }

    @Override
    public DimDepartment fetchById(String id) {
        return departmentMapper.findOneById(id);
    }
}
